package nz.ac.massey.groupproject.ecs2;

import java.util.ArrayList;

public class CollisionSystem implements ECSSystem {
	// TODO






	public static boolean isColliding(Position pos1, Polygon shape1, Position pos2, Polygon shape2) {
		if (shape1 == null && shape2 == null)
			return pos1.x == pos2.x && pos1.y == pos2.y;
		if (shape1 == null) {
			var tmpp = pos1;    pos1 = pos2;      pos2 = tmpp;
			var tmps = shape1;  shape1 = shape2;  shape2 = tmps;
		}

		Polygon redzone = (shape2 == null) ? shape1 : Polygon.minkowskiDiff(shape1, shape2);
		var P = new Vec2l(pos2.x - pos1.x, pos2.y - pos1.y);
		return redzone.whereIs(P) < 0;
	}

	public static boolean collideWithWall(ArrayList<ECSEntity> walls, ECSEntity ent) {
		final var epos      = (Position)          ent.get(Position        .class);
		final var epolycomp = (PolygonComponent)  ent.get(PolygonComponent.class);
		final var epoly = (epolycomp == null) ? null : epolycomp.poly;
		assert epos != null;

		for (var wall : walls) {
			final var wpoly = ((PolygonComponent) wall.get(PolygonComponent.class)).poly;
			final var wpos  =  (Position)         wall.get(Position        .class);
			assert wpoly != null && wpos != null;

			if (isColliding(wpos, wpoly, epos, epoly)) return true;
		}
		return false;
	}
}
